﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using rehabilitation.Data;
using rehabilitation.Models;
namespace rehabilitation.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DoctorController : ControllerBase
    {
        private readonly AppDbContext _context;

        public DoctorController(AppDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 1. 医生注册
        /// </summary>
        [HttpPost("register")]
        public async Task<IActionResult> Register([FromBody] Doctor doctor)
        {
            if (await _context.Doctors.AnyAsync(d => d.Phone == doctor.Phone))
            {
                return BadRequest("手机号已注册");
            }

            doctor.CreateTime = DateTime.Now;
            _context.Doctors.Add(doctor);
            await _context.SaveChangesAsync();
            return Ok("注册成功");
        }

        /// <summary>
        /// 2. 医生登录
        /// </summary>
        [HttpPost("login")]
        public async Task<IActionResult> Login([FromBody] Doctor doctor)
        {
            var user = await _context.Doctors
                .FirstOrDefaultAsync(d => d.Phone == doctor.Phone && d.Password == doctor.Password);

            if (user == null)
            {
                return Unauthorized("手机号或密码错误");
            }

            return Ok(new { message = "登录成功", doctorId = user.Id });
        }

        /// <summary>
        /// 3. 修改医生个人信息（不含密码）
        /// </summary>
        [HttpPut("update")]
        public async Task<IActionResult> UpdateDoctor([FromBody] Doctor doctor)
        {
            var existingDoctor = await _context.Doctors.FindAsync(doctor.Id);
            if (existingDoctor == null)
            {
                return NotFound("医生不存在");
            }

            existingDoctor.Name = doctor.Name;
            existingDoctor.Sex = doctor.Sex;
            existingDoctor.Age = doctor.Age;
            existingDoctor.Avatar = doctor.Avatar;
            existingDoctor.Description = doctor.Description;
            existingDoctor.Phone = doctor.Phone;

            await _context.SaveChangesAsync();
            return Ok("信息修改成功");
        }

        /// <summary>
        /// 4. 修改医生密码（需要输入旧密码）
        /// </summary>
        [HttpPut("change-password")]
        public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordRequest request)
        {
            var doctor = await _context.Doctors.FindAsync(request.Id);
            if (doctor == null)
            {
                return NotFound("医生不存在");
            }

            if (doctor.Password != request.OldPassword)
            {
                return BadRequest("旧密码错误");
            }

            doctor.Password = request.NewPassword;
            await _context.SaveChangesAsync();
            return Ok("密码修改成功");
        }

        /// <summary>
        /// 5. 通过 ID 查询医生详细信息
        /// </summary>
        [HttpGet("{id}")]
        public async Task<IActionResult> GetDoctorById(int id)
        {
            var doctor = await _context.Doctors.FindAsync(id);
            if (doctor == null)
            {
                return NotFound("医生不存在");
            }

            return Ok(doctor);
        }
    }

    public class ChangePasswordRequest
    {
        public int Id { get; set; }
        public string OldPassword { get; set; }
        public string NewPassword { get; set; }
    }
}
